package com.polaris.live.dialog

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.CollectionUtils
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.AwardProgressAdapter
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.common.util.DateUtils
import com.polaris.live.databinding.DialogAwardProgressBinding
import com.polaris.live.resp.back_resp.LiveSubsidyIndicatorResp
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

/**
 * AwardProgressDialog
 *
 * @author Created by 半仙 on 2024/1/18/018 20:06
 */
class AwardProgressDialog(
    context: Context,
    private val mLiveSubsidyIndicatorResp: LiveSubsidyIndicatorResp,
    private val mIsHomeCome: Boolean = false,
    private val mStartLive: (() -> Unit)? = null,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogAwardProgressBinding

    private var mAdapter: AwardProgressAdapter? = null
    override fun getImplLayoutId() = R.layout.dialog_award_progress

    private var timeJob: Job? = null

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogAwardProgressBinding.bind(popupImplView)
        val mWaitTime = mLiveSubsidyIndicatorResp.waitTime
        mAdapter = AwardProgressAdapter(mWaitTime)
        mBinding.recycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        }
        val rule = mLiveSubsidyIndicatorResp.rule as MutableList
        if (!CollectionUtils.isEmpty(rule)) {
            rule.removeAt(0)
            val lastDate = rule.getOrNull(rule.size - 1)
            if (lastDate != null) {
                rule.removeAt(rule.size - 1)
            }
            mAdapter?.setList(mLiveSubsidyIndicatorResp.rule)
            mBinding.tvTime.text = lastDate?.name

            val liveTime = lastDate?.liveTime
            if (liveTime != null) {
                if (mLiveSubsidyIndicatorResp.tomorrowSubsidy && mLiveSubsidyIndicatorResp.waitTime >= liveTime) {
                    mBinding.llDetailMask.visibility = View.VISIBLE
                }

                if (mWaitTime >= liveTime) {
                    mBinding.ivTitleChestImg.setImageResource(R.drawable.anchor_progress_open_alpha)
                }
            }
        }
        var times = mLiveSubsidyIndicatorResp.endTime
        if (times >= CacheUtil.getServerTime()) {
            times -= CacheUtil.getServerTime()
            timeJob = CoroutineScopeManager.mainScope.launch {
                while (isActive && times >= 1000L) {
                    val millis2String =
                        DateUtils.formatDuration(times).convertArabicDigitsToNormal()
                    val split = millis2String.split(":")
                    if (AppCodeUtils.isAR()) {
                        mBinding.timeHour.text = split[2]
                        mBinding.timeMinute.text = split[1]
                        mBinding.timeSecond.text = split[0]
                    } else {
                        mBinding.timeHour.text = split[0]
                        mBinding.timeMinute.text = split[1]
                        mBinding.timeSecond.text = split[2]
                    }
                    times -= 1000L
                    delay(1000)
                }
                dismiss()
            }
        }
        mBinding.ivClose.setOnSingleClickListener {
            dismiss()
        }

        if (mIsHomeCome) {
            mBinding.ivCenter.setImageResource(R.drawable.img_zhuanshujianglitanchuang_big)
            mBinding.goToLive.visibility = View.VISIBLE
            mBinding.tvCancel.visibility = View.VISIBLE
        }

        mBinding.goToLive.setOnSingleClickListener {
            mStartLive?.invoke()
            dismiss()
        }

        mBinding.tvCancel.setOnSingleClickListener {
            Router.builder(RoutePath.MYBEANS_ACTIVITY).navigation(context)
            dismiss()
        }
    }

    override fun onDismiss() {
        super.onDismiss()
        timeJob?.cancel()
        timeJob = null
    }
}